<template>
  <div>
    <!-- 查询条件 -->
    <el-card class="search-box" shadow="never">
      <el-form :model="par" inline class="demo-form-inline">
        <el-form-item label="审批状态：">
          <el-select v-model="par.AppStatus" placeholder="请选择" clearable>
            <el-option v-for="item in ApproveStatusData" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="姓名：">
          <el-input v-model="par.NickName" placeholder="请输入姓名" clearable />
        </el-form-item>

        <el-form-item label="请假时间：">
          <el-date-picker v-model="timeRange" type="daterange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="YYYY-MM-DD" @change="handleDateChange" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="nickName" label="姓名" align="center" width="130" />
      <el-table-column prop="depId" label="部门" align="center" width="100">
        <template v-slot="scoped">
          <span> {{depVule[0].children.find((x: any) => x.value == scoped.row.depId)?.label}} </span>
        </template>
      </el-table-column>
      <el-table-column prop="appStatus" label="审批状态" align="center">
        <template v-slot="scoped">
          {{ApproveStatusData.find((x: any) => x.value == scoped.row.appStatus)?.label}}
        </template>
      </el-table-column>
      <el-table-column prop="leaveType" label="请假别类" align="center" width="100" />
      <el-table-column prop="leavePeriod" label="请假时间" align="center" width="330" />
      <el-table-column prop="leaveTime" label="总工时(H)" align="center" width="100" />
      <el-table-column prop="leaveEndTime" label="报道时间" align="center" width="160">
        <template v-slot="scoped">
          {{ scoped.row.leaveEndTime ? moment(scoped.row.leaveEndTime).format("yyyy-MM-DD HH:mm:ss") : "空" }}
        </template>
      </el-table-column>
      <el-table-column prop="leaveReason" label="请假事由" align="center" width="130" />
      <el-table-column prop="leaveLeader" label="部门领导" align="center" width="200" />
      <el-table-column prop="createTime" label="创建时间" align="center" width="160">
        <template v-slot="scoped">
          {{ scoped.row.createTime ? moment(scoped.row.createTime).format("yyyy-MM-DD HH:mm:ss") : "空" }}
        </template>
      </el-table-column>
      <el-table-column prop="leaveLeader" label="" align="center" width="200" />
      <el-table-column fixed="right" label="操作" align="center" min-width="155">
        <template v-slot="scoped">
          <el-button link type="warning" size="small" @click="Updates(scoped.row)"
            v-show="scoped.row.appStatus === 1 && scoped.row.userId === store.ccstore.userId">修改</el-button>
          <el-button link type="danger" size="small" @click="Del(scoped.row.leaveId)"
            v-show="scoped.row.appStatus === 1 && scoped.row.userId === store.ccstore.userId">删除</el-button>
          <el-button link type="primary" size="small" @click="Approves(scoped.row)"
            v-show="store.ccstore.levelRoleId == scoped.row.levelRoleId && scoped.row.appStatus != 3">审批</el-button>
          <el-button link type="primary" size="small" @click="Approves2(scoped.row)">查看详细</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination v-model:current-page="page.TotalIndex" v-model:page-size="page.TotalSize"
      :page-sizes="[1, 5, 10, 15, 20, 25, 30]" background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount" />
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus';
import moment from 'moment';
import { compile, computed, onMounted, reactive, ref, watch } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { useRouter, useRoute } from 'vue-router';
import { useCounterStore } from '@/stores/counter';
import DepData from '@/stores/departmentData'
import axios from 'axios';
import ApproveStatus from '@/stores/AppStatus'
// 新增时间范围响应式变量
const timeRange = ref([])
// 处理日期选择
const handleDateChange = (dates: any) => {
  if (dates && dates.length === 2) {
    par.StartTime = dates[0]
    par.EndTime = dates[1]
  } else {
    par.StartTime = ''
    par.EndTime = ''
  }
}
// 查询处理
const handleSearch = () => {
  page.TotalIndex = 1 // 重置到第一页
  show()
}

// 重置处理
const handleReset = () => {
  par.AppStatus = ''
  par.NickName = ''
  par.StartTime = ''
  par.EndTime = ''
  timeRange.value = []
  page.TotalIndex = 1
  show()
}
//审批状态
const ApproveStatusData = ApproveStatus;
//部门信息
const depVule = DepData;
//pini保存
const store = useCounterStore();
//跳转
const router = useRouter();
//传参
const route = useRoute();


//#region  审批
const Approves = (data: any) => {
  router.push({
    name: "AppLeaveModel",
    query: data
  })
}
const Approves2 = (data: any) => {
  router.push({
    name: "AppLeaveModel2",
    query: data
  })
}
//#endregion


//#region 修改跳转
//跳转修改
const Updates = (data: any) => {
  router.push({
    name: "UpdateLeaveModel",
    query: data
  })
}
//#endregion

const Del = (ids: any) => {
  ElMessageBox.confirm(
    '你确定删除吗?',
    '提示',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      axios.post("https://localhost:7272/api/Approve/DelLeaveRecordModel", {
        "leaveId": ids
      }).then(res => {
        if (res.data.code == 204) {
          ElMessage.success(res.data.msg);
          show();
        }
        show();
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消',
      })
    })
}


//#region 显示

//条件查询
const par = reactive({
  appStatus: '',
  StartTime: '',
  EndTime: '',
  NickName: '',
})

//分页
const page = reactive({
  TotalIndex: 1,
  TotalSize: 15,
  totalCount: 0,
  totalPage: 0,
})

//存储请假申请表数据集合
const tableData = ref([])
//获取集合数据
const show = () => {
  axios.get("https://localhost:7174/api/Approve/ListLeaveModel", {
    params: {
      LevelRoleId: store.ccstore.levelRoleId,
      UserId: store.ccstore.userId,
      DepId: store.ccstore.depId,
      AppStatus: par.appStatus,
      // 确保传递的是请假时间
      StartTime: par.StartTime,
      EndTime: par.EndTime,
      NickName: par.NickName,
      TotalIndex: page.TotalIndex,
      TotalSize: page.TotalSize
    }
  }).then(res => {
    tableData.value = res.data.items
    page.totalCount = res.data.totalCount
    page.totalPage = res.data.totalPage
  })
}

onMounted(() => {
  show();
})

watch(page, () => { show(); })

//#endregion
</script>

<style scoped>
/* 分页 */
.demo-pagination-block+.demo-pagination-block {
  margin-top: 10px;
}

.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.search-box {
  margin-bottom: 20px;
  background: #f5f7fa;
  border-radius: 4px;
}

.demo-form-inline :deep(.el-form-item) {
  margin-right: 30px;
  margin-bottom: 0;
}

.demo-form-inline :deep(.el-select) {
  width: 200px;
}

.demo-form-inline :deep(.el-date-editor) {
  width: 350px;
}
</style>